'use strict';

const db = uniCloud.database();
exports.main = async (event, context) => {
	let {relateUuid, wordBankId, reviewCount} = event;
	
	const getRes = await db.collection('memorization').where({
		relateUuid,
		wordBankId
	}).get();
	
	const todayNeedReviewNum = getRes.data[0].todayNeedReviewNum;
	
	if(todayNeedReviewNum === 0){
		return getRes
	}
	
	if(todayNeedReviewNum > reviewCount) {
		const updateRes = await db.collection('memorization').where({
			relateUuid,
			wordBankId
		}).update({
			todayNeedReviewNum: todayNeedReviewNum - reviewCount
		});
		
		return updateRes;
	} else {
		const updateRes = await db.collection('memorization').where({
			relateUuid,
			wordBankId
		}).update({
			todayNeedReviewNum: 0
		});
		
		return updateRes;
	}

	
	
};
